package com.kamistoat.meimeimall.meimeimallproduct.web;

import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.annotation.LoginRequired;
import com.kamistoat.meimeimall.common.utils.PageUtils;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.common.valid.BrandAddGroup;
import com.kamistoat.meimeimall.common.valid.BrandSTUpdateGroup;
import com.kamistoat.meimeimall.common.valid.BrandWholeUpdateGroup;
import com.kamistoat.meimeimall.meimeimallproduct.entity.BrandEntity;
import com.kamistoat.meimeimall.meimeimallproduct.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@Api(tags = "品牌管理")
@RestController
@RequestMapping("web")
public class BrandManagePageController {
    protected Logger logger = LoggerFactory.getLogger(BrandManagePageController.class);

    @Autowired
    BrandService brandService;

    @ApiOperation("生成商品域品牌的测试数据")
    @GetMapping("/construct_test_data_brand")
    // @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R constructTestBrandData(@RequestParam("brandNum") Integer brandNum) {
        brandService.constructTestBrandData(brandNum);
        return R.ok();
    }


    /**
     * 列表
     */
    @ApiOperation("从数据库获取列表")
    @GetMapping("/list")
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("从数据库查询指定POJO")
    @GetMapping("/info/{brandId}")
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @ApiOperation("向数据库保存POJO")
    @PutMapping("/brand_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:brand:save")
    // 使用BindingResult接收数据校验的错误异常，而如果删除掉BindingResult，异常被直接抛出，可以用@ControllerAdvice来接收。
    public R save(@Validated({BrandAddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/) {
//        if (result.hasErrors()) {
//            // 如果BindingResult中有错误，就获取出这些错误封装成map
//            Map<String, String> errorMap = new HashMap<>();
//            result.getFieldErrors().forEach((fieldError) -> {
//                // 遍历所有错误，拿出每一个错误的字段和错误消息提示，做成键值对放到errorMap中
//                errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
//            });
//            return R.error(400, "提交的数据不合法").put("errorMap", errorMap);
//        } else {
//            brandService.save(brand);
//            return R.ok();
//        }
        brandService.save(brand);
        return R.ok();
    }

    @ApiOperation("向数据库更新POJO")
    @PostMapping("/brand")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:brand:update")
    public R update(@Validated({BrandWholeUpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);
        return R.ok();
    }

    /**
     * 仅仅修改状态
     */
    @ApiOperation("修改品牌状态")
    @PostMapping("/update_brand_status")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:brand:update")
    public R updateStatus(@Validated({BrandSTUpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("从数据库删除POJO")
    @DeleteMapping("/brand_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));
        return R.ok();
    }
}
